package com.wechart.service.system.impl;

import com.wechart.dao.domain.Systeminfo;
import com.wechart.dao.domain.SysteminfoCriteria;
import com.wechart.dao.mapper.SysteminfoMapper;
import com.wechart.model.SystemModel;
import com.wechart.service.system.SysteminfoService;
import com.wechart.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service(value = "systeminfoService")
public class SysteminfoServiceImpl implements SysteminfoService {

    @Autowired
    private SysteminfoMapper systeminfoMapper;

    @Override
    public List<Systeminfo> getSysteminfo(SystemModel systemModel) {
        SysteminfoCriteria systeminfoCriteria=new SysteminfoCriteria();
        SysteminfoCriteria.Criteria criteria=systeminfoCriteria.createCriteria();
        if(StringUtils.isNoNullString(systemModel.getStart())){
            criteria.andWorkdateGreaterThan(StringUtils.cutDateStr(systemModel.getStart()));
        }
        if(StringUtils.isNoNullString(systemModel.getEnd())){
            criteria.andWorkdateLessThan(StringUtils.cutDateStr(systemModel.getEnd()));
        }
        if(StringUtils.isNoNullString(systemModel.getCode())){
            criteria.andOrgcodeEqualTo(systemModel.getCode());
        }
        systeminfoCriteria.setOrderByClause("workdate desc");
        return systeminfoMapper.selectByExample(systeminfoCriteria);
    }

    /**
     * 系统信息插入
     * @param systemModel
     * @return
     */
    @Override
    public int insertSysteminfo(SystemModel systemModel) {
        SysteminfoCriteria systeminfoCriteria=new SysteminfoCriteria();
        Systeminfo data = new Systeminfo();
//        data.setOrgcode(systemModel.getOrgcode());
        data.setBankcode(systemModel.getBankcode());
        data.setWorkdate(StringUtils.cutDateStr(systemModel.getWorkdate()));
        data.setPrevdate(StringUtils.cutDateStr(systemModel.getPrevdate()));
        data.setNextdate(StringUtils.cutDateStr(systemModel.getNextdate()));
        data.setNetround(Short.parseShort(systemModel.getNetround()));
        data.setState(Short.parseShort(systemModel.getState()));
        return systeminfoMapper.insertSelective(data);
    }

    /**
     * 系统信息修改
     * @param systemModel
     * @return
     */
    @Override
    public int modifySysteminfo(SystemModel systemModel ) {
        SysteminfoCriteria systeminfoCriteria=new SysteminfoCriteria();
        systeminfoCriteria.createCriteria().andOrgcodeEqualTo(systemModel.getOrgcode());
        Systeminfo data = new Systeminfo();
//        data.setOrgcode(systemModel.getOrgcode());
        data.setBankcode(systemModel.getBankcode());
        data.setWorkdate(StringUtils.cutDateStr(systemModel.getWorkdate()));
        data.setPrevdate(StringUtils.cutDateStr(systemModel.getPrevdate()));
        data.setNextdate(StringUtils.cutDateStr(systemModel.getNextdate()));
        data.setNetround(Short.parseShort(systemModel.getNetround()));
        data.setState(Short.parseShort(systemModel.getState()));
        return systeminfoMapper.updateByExampleSelective(data,systeminfoCriteria);
    }

    /**
     * 根据id查询
     * @param orgcode
     * @return
     */
    @Override
    public Systeminfo getSysteminfoById(String orgcode) {
        SysteminfoCriteria systeminfoCriteria=new SysteminfoCriteria();
        systeminfoCriteria.createCriteria().andOrgcodeEqualTo(orgcode);
        List<Systeminfo> data = systeminfoMapper.selectByExample(systeminfoCriteria);
        if (null!=data) {
            return data.get(0);
        }else{
            return null;
        }
    }
}
